﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class Reisen : Form
    {
        static Dictionary<string, string> Index;
        string ID = "0";

        public Reisen()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            Point openUpHere = new Point(857, 0);
            this.Location = openUpHere;
            Index = new Dictionary<string, string>();
        }
        public Reisen(string index)
        {
            InitializeComponent();
            Index = new Dictionary<string, string>();
            ID = index;
        }

        private void Reisen_Load(object sender, EventArgs e)
        {
            using (OracleConnection con = new OracleConnection("Data Source=" + Form1.CONFIG_DB_HOST + "; User Id=" + Form1.CONFIG_DB_USER + "; Password=" + Form1.CONFIG_DB_PASS))
            {
                con.Open();
                OracleCommand com = con.CreateCommand();
                com.CommandText = "Select Name,Nummer FROM Locations";

                OracleDataReader odr = com.ExecuteReader();

                while (odr.Read())
                {
                    comboBox1.Items.Add(odr.GetString(0));
                    Index.Add(odr.GetValue(0).ToString(), odr.GetInt32(1).ToString());
                }
            }
            if(ID != "0")
                using (OracleConnection con = new OracleConnection("Data Source=" + Form1.CONFIG_DB_HOST + "; User Id=" + Form1.CONFIG_DB_USER + "; Password=" + Form1.CONFIG_DB_PASS))
                {
                    con.Open();
                    OracleCommand com = con.CreateCommand();
                    com.CommandText = "Select r.Nummer, r.Beschreibung, l.Name, r.Preis, r.PLAETZE FROM REISEN r, Locations l WHERE l.Nummer = r.Location";

                    OracleDataReader odr = com.ExecuteReader();

                    while (odr.Read())
                    {
                        textBox1.Text = odr.GetValue(0).ToString();
                        textBox3.Text = odr.GetValue(1).ToString();
                        comboBox1.Text =odr.GetValue(2).ToString();
                        textBox4.Text = odr.GetValue(3).ToString();
                        textBox5.Text = odr.GetValue(4).ToString();
                    }
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "" && ID == "0" && comboBox1.SelectedIndex >= 0)
            {
                try
                {
                    using (OracleConnection con = new OracleConnection("Data Source=" + Form1.CONFIG_DB_HOST + "; User Id=" + Form1.CONFIG_DB_USER + "; Password=" + Form1.CONFIG_DB_PASS))
                    {
                        con.Open();
                        OracleCommand com = con.CreateCommand();
                        com.CommandText = "INSERT INTO Reisen VALUES(SQ_Reisen.NEXTVAL, '" + textBox3.Text + "', " + Index[comboBox1.Text] + ",  " + textBox4.Text + ", " + textBox5.Text + ")";

                        com.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                Close();
            }
            if (textBox3.Text != "" && ID != "0")
            {
               
                    using (OracleConnection con = new OracleConnection("Data Source=" + Form1.CONFIG_DB_HOST + "; User Id=" + Form1.CONFIG_DB_USER + "; Password=" + Form1.CONFIG_DB_PASS))
                    {
                        con.Open();
                        OracleCommand com = con.CreateCommand();
                        com.CommandText = "UPDATE Reisen SET Beschreibung = '" + textBox3.Text + "' , Location = " + Index[comboBox1.Text] + ", Preis = " + textBox4.Text + ", Plaetze = " + textBox5.Text + " WHERE Nummer = " + ID;

                        com.ExecuteNonQuery();
                    }
                

                Close();
            }
        }
    }
}
